package com.lz.sports.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import androidx.lifecycle.lifecycleScope
import com.lz.library.base.BaseActivity
import com.lz.library.extension.startActivity
import com.lz.sports.BR
import com.lz.sports.R
import com.lz.sports.databinding.ActWelcomeBinding
import com.lz.sports.service.UploadService
import com.lz.sports.vm.WelcomeViewModel
import com.lz.ub.UBCService
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class WelcomeActivity : BaseActivity<WelcomeViewModel, ActWelcomeBinding>() {

    companion object {

        //重启APP
        fun restart(context: Context) {
            val intent = Intent(context, WelcomeActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
            context.startActivity(intent)
        }
    }

    override fun getLayoutRes(): Int = R.layout.act_welcome

    override fun getVMVariableId(): Int = BR.vm

    override fun initData(savedState: Bundle?) {
        lifecycleScope.launch {
            delay(1500)
            startActivity(MainActivity::class.java)
            finish()
        }
        UBCService.start(this)
        UploadService.start(this)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean { //不可返回
        return if (keyCode == KeyEvent.KEYCODE_BACK) {
            true
        } else super.onKeyDown(keyCode, event)
    }
}